<template>
    <div>
        添加分类
        <van-field  label='名称' v-model='name' placeholder="请输入名称"/>
        <van-field  label='参数' v-model='params'  placeholder="请输入参数"/>
        选择分类
        <reply :data='catelist'/>
        <van-button @click="add">添加</van-button>

    </div>
</template>

<script>
//导入子组件
import reply from './reply'
//注册子组件
    export default {
        components:{
            'reply':reply
        },
        data(){
            return{
                pid:0,
                name:'',
                params:'',
                catelist:{},
            }
        },
        methods:{
            add(){
                //判断参数是否有值
                if(this.params){
                    //有的话转字符串类型
                    this.params=JSON.stringify(this.params)
                }
                //判断有没有取出pid取出来就更新上面定义的pid 没有还为0
                if(localStorage.getItem('pid')){
                    this.pid=localStorage.getItem('pid')
                }
                let data={'name':this.name,'pid':this.pid,'params':this.params}
                this.$axios.post('cateM',data)
                .then(res=>{
                    if(res.data.code==200){
                        alert('添加成功')
                         get_catelist()
                    }
                   
                })
                
            },
            get_catelist(){
                this.$axios.get('cateM')
                .then(res=>{
                    this.catelist={'id':0,'name':'333','son':res.data.list}
                    console.log(res.data.list)
                })
            },
        },
        mounted(){
            this.get_catelist()
        },
    }
</script>

<style scoped>

</style>